﻿--[[
   @brief    comment_invite control
   @author   jeffrywang
   @version  1.0
   @date     2017/02/14
--]]

-- 玩家游戏时长
RailGamePlayedDuration = {
    -- TODO(jeffrywang) 需要根据OSS获取
    trrigger_duration = 10*60*60,
    --[[
    [game_id] = {
      duration = 0, -- 游戏时长
      has_comment = false, -- 是否评论过
    }
    --]]
};

comment_invite_control = {
    kUrlRequestDuration = LoadStr("COMMENT_INVITE_DRUATION_URL");
    kUrlRequestGameTime = LoadStr("COMMENT_INVITE_GAMETIME_URL");
    kUrlCommentSwitch = LoadStr("GL_GAME_CONFIG_URL");
    comment_switchs = {};
    init = function()
    end;
};

function comment_invite_control.on_login_success()
    comment_invite_control.request_game_comment_config(0);
end

function comment_invite_control.is_comment_enabled(game_id)
    for i, v in pairs(comment_invite_control.comment_switchs) do
        if v["game_id"] == tostring(game_id) then
            return v["switch"] ~= "1";
        end
    end
    return true;
end

function comment_invite_control.request_game_comment_config(start_index)
    if nil == start_index then
        start_index = 0;
    end
    
    local client_type = 1;
    if base_utility.is_type_of_oversea_ver() then
        client_type = 2;
    end

    local post_data = {
            data_names = "game_comment_conf",
            command = "list_all",
            params = {
                start_page = start_index,
                items_per_pager = 50
            },
            stamp = {
                credential = {
                    agent_client_type = client_type,
                },
                agent_client_language = base_utility.get_currrent_language_str();
            }
        };
    base_utility.curl_request_async(comment_invite_control.kUrlCommentSwitch
    , "comment_invite_control.on_request_game_comment_config"
    , false, nil, true, nil, base_utility.table_to_json(post_data));
end

function comment_invite_control.on_request_game_comment_config(response)

    base_utility.log("[comment_invite_control]game comment config ".. response);
    local res_tbl = base_utility.json_to_table(response);
    if nil == res_tbl or nil == res_tbl.result then
        return;
    end
    if 0 ~= tonumber(res_tbl.result.error_code) then
        return;
    end

    local items_count = 0;
    if nil ~= res_tbl.items and type(res_tbl.items) == "table" then
        items_count = #res_tbl.items;
        for k, v in pairs(res_tbl.items) do
            table.insert(comment_invite_control.comment_switchs, v);
        end
    end

    -- request more
    local total_items = tonumber(res_tbl.total_items or "0");
    local start_page = tonumber(res_tbl.start_page or "0");
    local items_per_page = tonumber(res_tbl.items_per_pager or "0");

    if total_items > (start_page * items_per_page + items_count) then
        comment_invite_control.request_game_comment_config(start_page + 1);
    end
end

function comment_invite_control.on_game_start(event_id, data) 
    base_utility.log("[comment_invite_control]on_game_start");
    local info = base_utility.json_to_table(data);
    if info and info.game_id then
        local game_id = info.game_id;
        if not comment_invite_control.is_comment_enabled(game_id) then
            base_utility.log("[comment_invite_control]on_game_start,Comment disabled!".. game_id);
            return;
        end
        comment_invite_control.request_played_duration(game_id, true);
    end
end;

function comment_invite_control.on_game_end(event_id, data)
    base_utility.log("[comment_invite_control]on_game_end");
    local info = base_utility.json_to_table(data);
    if info and info.game_id then
        local game_id = info.game_id;
        if not comment_invite_control.is_comment_enabled(game_id) then
            base_utility.log("[comment_invite_control]on_game_end,Comment disabled!".. game_id);
            return;
        end
        comment_invite_control.request_played_duration(game_id, false);
    end
end;

function comment_invite_control.request_played_duration(game_id, is_starting)
    base_utility.log("[comment_invite_control]request_played_duration, game_id=".. game_id);
    if game_id == nil or game_id == 0 or is_starting == nil then
        return
    end
    
    local game_lib = base_utility.get_component_adapter("IGame_library",
             "ierd_tgp.game_library.Get_game_library_adapter();");
    if game_lib == nil then
          return
    end
    local channel = game_lib:get_game_distribution_channels(game_id);
    if banner_model.distribution_channels.CHANNELS_RAIL ~= channel then
        -- 非单机游戏,不关注
        return
    end
    
    if RailGamePlayedDuration[game_id] == nil then
        RailGamePlayedDuration[game_id] = {duration = 0, has_comment = false};
    end

    local game_duration = RailGamePlayedDuration[game_id].duration;
    local has_comment = RailGamePlayedDuration[game_id].has_comment;
    if not has_comment then -- 没有评论标记
      if (game_duration == nil or 
        game_duration < RailGamePlayedDuration.trrigger_duration) then -- 时长尚未达成
        -- 请求玩家当前游戏时长
        local url = string.format(comment_invite_control.kUrlRequestDuration, 
                                  tostring(game_id), tostring(game_id));
        local login_cfg_url = url;
        local user_data = {game_id = game_id, is_starting = is_starting};
        base_utility.curl_request_async(login_cfg_url, 
          "comment_invite_control.on_request_game_duration", true,
          nil, true, base_utility.table_to_json(user_data));
      end
    else
        base_utility.log("[comment_invite_control]request_played_duration, game has comment, game_id=".. game_id);
    end
end;

function comment_invite_control.on_request_game_duration(s_json, callback_user_data)
    base_utility.log("[comment_invite_control]on_request_game_duration :".. s_json);
    local data = base_utility.json_to_table(callback_user_data);

    if nil ~= s_json and string.len(s_json) > 0 then
        local rsp_table = base_utility.json_to_table(s_json);
        
        local game_id = data.game_id;
        local game_duration = 0;
        -- 防护后台返回错误的问题
        if rsp_table and rsp_table.DuringTime then
            game_duration = tonumber(rsp_table.DuringTime) or 0;
        end
        local tgp_id = base_utility.get_tgp_id();
        if data.is_starting == false and -- 关闭游戏的情况下,判断游戏时长
            RailGamePlayedDuration[game_id].duration < RailGamePlayedDuration.trrigger_duration and
            game_duration > RailGamePlayedDuration.trrigger_duration then
            -- 查询评论数
            local url = string.format(comment_invite_control.kUrlRequestGameTime,
                tostring(game_id), tgp_id, tostring(game_id), tostring(game_id));
            local login_cfg_url = url;
            local user_data = {game_id = game_id};
            base_utility.curl_request_async(login_cfg_url, 
                "comment_invite_control.on_request_game_comment", true,
                nil, true, base_utility.table_to_json(user_data));
            -- 
        end
        -- 更新玩家游戏时长
        RailGamePlayedDuration[game_id].duration = game_duration;
    end
end;

function comment_invite_control.on_request_game_comment(s_json, callback_user_data)
    base_utility.log("[comment_invite_control]on_request_game_comment:".. s_json);
    local data = base_utility.json_to_table(callback_user_data);
    if nil ~= s_json and string.len(s_json) > 0 then
        local rsp_table = base_utility.json_to_table(s_json);
        local game_id = data.game_id;
        local rsp_gameid = tonumber(data.game_id);
        if game_id == rsp_gameid then
            local has_comment = ((rsp_table.comments ~= nil) and
                (next(rsp_table.comments) ~= nil));
            RailGamePlayedDuration[game_id].has_comment = has_comment;
            if not has_comment then
                comment_invite_control.show_comment_invite_tips(game_id);
            end
        end
    end
end;

function comment_invite_control.show_comment_invite_tips(game_id)
    base_utility.log("[comment_invite_control]show_comment_invite_tips:".. game_id);
    if not comment_invite_control.is_comment_enabled(game_id) then
        base_utility.log("[comment_invite_control]show_comment_invite_tips,Comment disabled!".. game_id);
        return;
    end
    local game_lib =
         base_utility.get_component_adapter("IGame_library",
             "ierd_tgp.game_library.Get_game_library_adapter();");
    if game_lib == nil then
          return;
    end;
    local game_info = ierd_tgp.game_library.GameDetailInfo:new_local();
    game_lib:get_game_detail_info(game_id, game_info);
    local game_name = game_info.name;
    local vendor_name = game_info.developer_info.name;
    local descTitle = '<font>'..LoadStr("COMMENT_INVITE_INVITATION_FROM_DEV")..'</font>';
    local desc = '<font c="0x00baff" bold="1">'..game_name.. 
        '</font> <font>'..LoadStr("COMMENT_INVITE_WORDING")..'</font><br>';

    local link = { button_text = LoadStr("COMMENT_INVITE_COMMENT"),
        type="jump_url", 
        url = LoadStr("COMMENT_INVITE_GAME_DETAIL") .. 
          tostring(game_id) .. "&addreview=1"};
    local link_str = base_utility.table_to_json(link);
    local info = {type = 0, icon = "", descTitle = descTitle, desc = desc, link = link_str};
    server_tips_control:show_comment_invite_tips(info);
end;

function comment_invite_control.on_cloudgaming_state_changed(param)
    if param == nil or param.game_id == nil or param.game_id == 0 or param.exit == nil then
        return
    end
    local game_id = param.game_id;
    if not comment_invite_control.is_comment_enabled(game_id) then
        base_utility.log("[comment_invite_control]on_cloudgaming_state_changed,Comment disabled!"
                            .. game_id);
        return;
    end

    if param.exit == 0 then
        comment_invite_control.request_played_duration(game_id, true);
    else
        comment_invite_control.request_played_duration(game_id, false);
    end
end;

comment_invite_control.init();
